<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DynamicState;
use App\Models\Message;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class DynamicStateController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DynamicState(['user:id,avatar,nickname']), function (Grid $grid) {
            $grid->model()->where('type',1);
            $grid->column('id')->sortable();
            $grid->column('sort');
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('title');

            $grid->column('media_type')
                ->using([1 => '图片',2 => '视频'])
                ->label([1 => 'primary',2 => 'success']);
            $grid->column('status')
                ->using([0 => '隐藏',1 => '显示'])
                ->label([0 => 'danger',1 => 'success']);
            $grid->column('audit_status')
                ->using([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->fixColumns(1);

            $grid->filter(function (Grid\Filter $filter) {

                $filter->like('user.nickname','用户昵称')
                       ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DynamicState(), function (Show $show) {
            $show->field('id');
            $show->field('sort');
            $show->field('user_id');
            $show->field('title');
            $show->field('media_type');
            $show->field('media');
            $show->field('content');
            $show->field('status');
            $show->field('audit_status');
            $show->field('reject_reason');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DynamicState(), function (Form $form) {
            $form->display('id');
            $form->number('sort')
                ->min(0)
                ->help('数字越小排序越靠前')
                ->required();
            $form->hidden('type');
            $form->text('title')
                ->maxLength(255)
                ->required();
            $form->textarea('content')
                ->required();
            $form->radio('media_type')
                ->options([1 => '图片',2 => '视频'])
                ->when(1,function (Form $form){
                    $form->multipleImage('media_url','图片')
                        ->disable();
                })->when(2,function (Form $form){
                    $form->display('media_url','视频')
                        ->customFormat(function ($value){
                            $html = '';
                            foreach ($value as $v){
                                $html .= '<video width="320" height="240" controls>
    <source src="'.$v.'" type="video/mp4">
</video>';
                                return $html;
                            }
                        });
                })
                ->default(1)
                ->required();

            $form->radio('status')
                ->options([1 => '显示',0 => '隐藏'])
                ->default(1)
                ->required();
            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->default(1)
                ->when(3,function (Form $form){
                    $form->textarea('reject_reason')
                        ->saving(function ($value){
                            return (string)$value;
                        })->rules('max:255',['max' => '最多500字']);
                })->required();

            $form->saving(function (Form $form){
                $form->input('type',1);
            });


            $form->saved(function (Form $form,$result){
                if ($result){
                    if (!$form->input('audit_time') && $form->input('audit_status')==2){
                        DB::table('articles')
                            ->where('id',$form->getKey())
                            ->update([
                                'audit_time' => date('Y-m-d'),
                            ]);
                        if ($form->input('user_id')){
                            event(new \App\Events\Article($form->model()));
                            $company_name = DB::table('firms')
                                ->where('user_id',$form->input('user_id'))
                                ->value('name');
                            if ($company_name){
                                Message::query()->create([
                                    'type' => 4,
                                    'content' => $company_name. '公司发布了新动态'
                                ]);
                            }
                        }

                    }


                    if ($form->input('user_id') && $form->input('audit_status')==2){
                        event(new \App\Events\Message([
                            'user_id' =>  $form->input('user_id'),
                            'type' => 2,
                            'content' => '恭喜您发布的动态已通过审核'
                        ]));
                    }

                    if ($form->input('user_id') && $form->input('audit_status')==3){
                        event(new \App\Events\Message([
                            'user_id' =>  $form->input('user_id'),
                            'type' => 2,
                            'content' => '您发布的动态未通过审核'.$form->input('reject_reason')
                        ]));
                    }


                }
            });




            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
